package run.scenario;

import static log.LogConstants.*;
import static run.scenario.GeneratorConstants.COMMENT;
import static run.scenario.GeneratorConstants.ExpName;
import static run.scenario.GeneratorConstants.LN;
import static run.scenario.GeneratorConstants.SEPARATOR;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import run.ScenarioManager;

public class ScenarGenerator {
	
	public static final int VALUE = 0;
	public static final int NAME = 1;
	
	public static final String EXPE_NAME = "VHF Picture Naming";
	protected static BufferedWriter writer;
	
	// Unused for now
	public static final int NbSujets = 80;
	
	public static final int NB_NAMING = 16;
	
	public static final boolean VERBOSE = true;
	
	public static void speak(String s) {
		if (VERBOSE)
			System.out.println(s);
	}
	
	public static final String[] ValeursImages = new String[] {	
		"House",
		"Boat",
		"Tree",
		"Book",
		"Lamp"
	};
	
	public static final String[] ValeursSides = {
		"Left",
		"Right"
	};
	
	public static void main(String[] args) {
		
		/* 
		 * Generating the images ordering.
		 * Each image will be shown NB_NAMING times, half on each side 
		 */
		ArrayList<int[]> ordering = new ArrayList<int[]>(0);
		int otherIndex;
		
		for (int index = 0 ; index < ValeursImages.length ; index++) {
			
			for (int n = 0 ; n < NB_NAMING ; n++) {
				
				for (int side = 0 ; side < ValeursSides.length ; side++) {
				
					do {
						otherIndex = (int) (Math.random() * ValeursImages.length);
					} while (otherIndex == index || otherIndex == ValeursImages.length);
					
					int[] result = new int[3];
					result[2] = side;
					result[side] = index;
					result[(side + 1) % ValeursSides.length] = otherIndex;
					
	//				speak(result[0] + ", " + result[1] + ", " + result[2]);
					
					ordering.add(result);
					
				}
			}
		}
		
		Collections.shuffle(ordering);
		
		try {
			
			writer = new BufferedWriter(new FileWriter("experiment_voice.txt"));
			
			/*
			 * Constants
			 */
			
			appendln(ExpName, EXPE_NAME);
			appendln(NbTrials, ""+(ValeursSides.length * ValeursImages.length * NB_NAMING));
			
			appendln(Block, "1");
			
			for (int trial = 0 ; trial < ordering.size() ; trial++) {
				
				appendln(Trial, ""+(trial+1), 
						LeftImage, ""+ordering.get(trial)[0],
						RightImage, ""+ordering.get(trial)[1],
						Reading, ""+ordering.get(trial)[2]);
				
			}
			
			writer.close();
			
			System.out.println("OK");
			
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	protected static void appendln(String... data) {
		
		try {
			
			for (int i = 0 ; i < data.length - 1 ; i++) {
				
				writer.append(data[i]);
				writer.append(SEPARATOR);
				
			}
			
			writer.append(data[data.length - 1]);
			writer.append(LN);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	protected static void comment(String comment) {
		try {
			writer.append(COMMENT + comment + LN);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
